import { Define, EntityObjectFactory } from 'tiny-entity2';
import { OrderItemDiscount } from './orderItemDiscount';
import { Product } from './product';
import { ModifierOption } from './modifierOption';
const EntityObjectType = EntityObjectFactory.GetEntityObjectType(process.env.dataBaseType || 'mysql');


@Define.Table({ TableName: "orderitem" })
export class OrderItem extends EntityObjectType<OrderItem>{
    @Define.PrimaryKey()
    id: string;

    @Define.Column({
        DataType: Define.DataType.VARCHAR,
        IsIndex: true
    })
    orderId: string

    @Define.Column({
        DataType: Define.DataType.VARCHAR,
        IsIndex: true
    })
    masterOrderId: string;

    @Define.Column({
        DataType: Define.DataType.VARCHAR,
        NotAllowNULL: true
    })
    productId: string;

    /**
     * 基础商品分类
     * 
     * @type {string}
     * @memberof OrderItem
     */
    @Define.Column({
        DataType: Define.DataType.VARCHAR
    })
    baseCategoryId: string;

    @Define.Column({
        DataType: Define.DataType.VARCHAR
    })
    baseCategoryName: string;

    @Define.Column({
        DataType: Define.DataType.VARCHAR,
        NotAllowNULL: true
    })
    productName: string;

    @Define.Column({
        DataType: Define.DataType.Decimal,
        DataLength: 10,
        DecimalPoint: 3,
        DefaultValue: 0
    })
    productPrice: number;

    @Define.Column({
        DataType: Define.DataType.VARCHAR
    })
    unitName: string;

    @Define.Column({
        DataType: Define.DataType.VARCHAR
    })
    refOrderItemId: string;

    @Define.Column({
        DataType: Define.DataType.BOOL,
        DefaultValue: false
    })
    isComboItem: boolean;

    @Define.Column({
        DataType: Define.DataType.BOOL,
        DefaultValue: false
    })
    isCombo:boolean;

    @Define.Column({
        DataType: Define.DataType.VARCHAR
    })
    note: string;

    @Define.Column({
        DataType: Define.DataType.Decimal,
        DefaultValue: 0,
        DataLength: 10,
        DecimalPoint: 3
    })
    qty: number;

    @Define.Column({
        DataType: Define.DataType.Decimal,
        DefaultValue: 0,
        DataLength: 10,
        DecimalPoint: 3
    })
    refundQty: number;

    @Define.Column({
        DataType: Define.DataType.Decimal,
        DefaultValue: 0,
        DataLength: 10,
        DecimalPoint: 3
    })
    freeQty: number;

    /**
     * 商品金额 * (qty - refundQty)
     * 
     * @type {number}
     * @memberof OrderItem
     */
    @Define.Column({
        DataType: Define.DataType.Decimal,
        DefaultValue: 0,
        DataLength: 10,
        DecimalPoint: 3
    })
    amount: number;

    /**
     * 商品附加价格
     * 
     * @type {number}
     * @memberof OrderItem
     */
    @Define.Column({
        DataType: Define.DataType.Decimal,
        DefaultValue: 0,
        DataLength: 10,
        DecimalPoint: 3
    })
    modifierAmount: number;

    @Define.Column({
        DataType: Define.DataType.Decimal,
        DefaultValue: 0,
        DataLength: 10,
        DecimalPoint: 3
    })
    discount: number;

    @Define.Column({
        DataType: Define.DataType.VARCHAR,
        DefaultValue: 'sales'
    })
    type: string;

    @Define.Column({
        DataType: Define.DataType.VARCHAR,
        DefaultValue: ''
    })
    status: string;

    @Define.Column({
        DataType: Define.DataType.VARCHAR,
        NotAllowNULL: true,
        IsIndex: true
    })
    storeId: string;

    @Define.Column({
        DataType: Define.DataType.BIGINT
    })
    version: number;

    @Define.Mapping({
        Mapping: 'OrderItemDiscount',
        MappingKey: 'orderItemId'
    })
    orderItemDiscounts: OrderItemDiscount[];

    product: Product;

    modifierOptions: ModifierOption[];

    comboOrderItems: OrderItem[];
}


export const OrderItemType = {
    Sales: 'sales',     //销售订单的单项
    Refund: 'refund'    //退单的单项
}

export const OrderItemStatus = {
    Refunded: 'refunded',//已退完
    Cancelled: 'cancelled'
}

